package com.molyfun.parents.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.molyfun.parents.MFPApplication;

/**
 * Created by skyfishjy on 1/5/15.
 */
public class PreferencesStorageUtil {
    private static final String PREFERENCES_NAME = "molyfun_parents_android";

    public static final String LOCAL_PUSH_LAST_LOGOUT_TIME = "last_logout_time";

    private static final String IS_NEW_USER = "is_new_user" + MFPApplication.sVersionName;

    private static final String TOKEN = "token";

    private static final String USER_BEAN = "user_bean";

    private static final String DEBUG_MODE = "debug_mode";

    private static final String SERVER_MODE = "server_mode";

    private static final String ORIGAN_OUT_SHOW_SPLASH = "origan_out_show_splash";

    private static final String IM_USER_NAME = "im_user_name";

    private static final String IM_USER_PWD = "im_user_pwd";

    private static final String IM_USER_PHO = "im_user_pho";

    private static final String IM_USER_ADDRESS = "im_user_address";

    private static final String IM_USER_EMAIL = "im_user_email";

    private static final String CURRENT_DEPARTNAME = "im_user_departname";

    private static final String CURRENT_STUDENTID = "student_id";

    private static final String THIRD_BINDING_STATE = "third_binding_state";

    private static final String THIRD_OPENID = "third_open_id";

    private static final String THIRD_ACCOUNT = "third_username";

    private static final String COURSE_SEARCH_HISTORY = "course_search_str_array";

    private static final String COURSE_PICK_SEARCH_STR = "course_pick_search_str";

    /**
     * 学员是否入驻机构
     */
    private static final String USER_ENTER = "user_enter";

    public interface ServerMode {
        public static int OFFICAL = 1000;
        public static int PREPARE = 1001;
        public static int TEST = 1002;
    }

    public static boolean isNewUser(Context context) {
        if (null == context) {
            return true;
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        return pref.getBoolean(IS_NEW_USER, true);
    }

    public static void setOldUser(Context context) {
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        Editor editor = pref.edit();
        editor.putBoolean(IS_NEW_USER, false);
        editor.apply();
    }

    public static String getToken(Context context) {
        if (null == context) {
            return "";
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        return pref.getString(TOKEN, "");
    }


    public static void setToken(Context context, String token) {
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        Editor editor = pref.edit();
        editor.putString(TOKEN, token);
        editor.apply();
    }

    public static String getUserBean(Context context) {
        if (null == context) {
            return "";
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        return pref.getString(USER_BEAN, "");
    }

    public static void setUserBean(Context context, String user_bean) {
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        Editor editor = pref.edit();
        editor.putString(USER_BEAN, user_bean);
        editor.apply();
    }

    public static void setLocalPushLastLogoutTime(Context context, long lastLogoutTime) {
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        Editor editor = pref.edit();
        editor.putLong(LOCAL_PUSH_LAST_LOGOUT_TIME, lastLogoutTime);
        editor.apply();
    }

    public static int getServerMode(Context context) {
        if (null == context) {
            return ServerMode.OFFICAL;
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        return pref.getInt(SERVER_MODE, ServerMode.OFFICAL);
    }

    public static void setServerMode(Context context, int serverMode) {
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putInt(SERVER_MODE, serverMode);
        editor.apply();
    }


    public static long getLocalPushLastLogoutTime(Context context) {
        if (null == context) {
            return 0l;
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        return pref.getLong(LOCAL_PUSH_LAST_LOGOUT_TIME, 0l);
    }

    public static void logout(Context context) {
        if (null == context) {
            return;
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        Editor editor = pref.edit();
        editor.remove(USER_BEAN);
        editor.remove(TOKEN);
        editor.remove(IM_USER_PHO);
        editor.remove(IM_USER_ADDRESS);
        editor.remove(IM_USER_EMAIL);
        editor.apply();
    }

    public static boolean isDebugMode(Context context) {
        if (null == context) {
            return false;
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        return pref.getBoolean(DEBUG_MODE, false);
    }

    public static void setDebugMode(Context context, boolean debug_mode) {
        if (null == context) {
            return;
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putBoolean(DEBUG_MODE, debug_mode);
        editor.apply();
    }

    public static boolean isUserEnter(Context context) {
        if (null == context) {
            return false;
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        return pref.getBoolean(USER_ENTER, true);
    }

    public static void setUserEnter(Context context, boolean debug_mode) {
        if (null == context) {
            return;
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putBoolean(USER_ENTER, debug_mode);
        editor.apply();
    }

    public static boolean isOriganOutShowSplash(Context context) {
        if (null == context) {
            return false;
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        return pref.getBoolean(ORIGAN_OUT_SHOW_SPLASH, false);
    }

    public static void setOriganOutShowSplash(Context context, boolean has_show_splash) {
        if (null == context) {
            return;
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putBoolean(ORIGAN_OUT_SHOW_SPLASH, has_show_splash);
        editor.apply();
    }

    public static String getIMUserName(Context context) {
        if (null == context) {
            return "";
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        return pref.getString(IM_USER_NAME, "");
    }

    public static void setIMUserName(Context context, String userName) {
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        Editor editor = pref.edit();
        editor.putString(IM_USER_NAME, userName);
        editor.apply();
    }

    public static String getIMUserPwd(Context context) {
        if (null == context) {
            return "";
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        return pref.getString(IM_USER_PWD, "");
    }

    public static void setIMUserPwd(Context context, String userPwd) {
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        Editor editor = pref.edit();
        editor.putString(IM_USER_PWD, userPwd);
        editor.apply();
    }

    public static void setPhoneNum(Context context, String phone) {
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        Editor editor = pref.edit();
        editor.putString(IM_USER_PHO, phone);
        editor.apply();
    }

    public static String getPhoneNum(Context context) {
        if (null == context) {
            return "";
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        return pref.getString(IM_USER_PHO, "");
    }

    public static void setAddress(Context context, String address) {
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        Editor editor = pref.edit();
        editor.putString(IM_USER_ADDRESS, address);
        editor.apply();
    }

    public static String getAddress(Context context) {
        if (null == context) {
            return "";
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        return pref.getString(IM_USER_ADDRESS, "");
    }

    public static void setEmail(Context context, String email) {
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        Editor editor = pref.edit();
        editor.putString(IM_USER_EMAIL, email);
        editor.apply();
    }

    public static String getEmail(Context context) {
        if (null == context) {
            return "";
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        return pref.getString(IM_USER_EMAIL, "");
    }

    public static void setDepartname(Context context, String departname) {
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        Editor editor = pref.edit();
        editor.putString(CURRENT_DEPARTNAME, departname);
        editor.apply();
    }
    public static String getDepartname(Context context) {
        if (null == context) {
            return "";
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        return pref.getString(CURRENT_DEPARTNAME, "");
    }
    public static void setStudentId(Context context, String studentId) {
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        Editor editor = pref.edit();
        editor.putString(CURRENT_STUDENTID, studentId);
        editor.apply();
    }
    public static String getStudentId(Context context) {
        if (null == context) {
            return "";
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        return pref.getString(CURRENT_STUDENTID, "");
    }
    public static void setThirdBindingStatu(Context context, String bindingState) {
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        Editor editor = pref.edit();
        editor.putString(THIRD_BINDING_STATE, bindingState);
        editor.apply();
    }
    public static String getThirdBindingStatu(Context context) {
        if (null == context) {
            return "";
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        return pref.getString(THIRD_BINDING_STATE, "");
    }
    public static void setThirdOpenId(Context context, String oponId) {
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        Editor editor = pref.edit();
        editor.putString(THIRD_OPENID, oponId);
        editor.apply();
    }
    public static String getThirdOpenId(Context context) {
        if (null == context) {
            return "";
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        return pref.getString(THIRD_OPENID, "");
    }
    public static void setThirdAccount(Context context, String username) {
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        Editor editor = pref.edit();
        editor.putString(THIRD_ACCOUNT, username);
        editor.apply();
    }
    public static String getThirdAccount(Context context) {
        if (null == context) {
            return "";
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        return pref.getString(THIRD_ACCOUNT, "");
    }

    public static void setCourseSearchHistory(Context context, String searchStr) {
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        Editor editor = pref.edit();
        editor.putString(COURSE_SEARCH_HISTORY, searchStr);
        editor.apply();
    }
    public static String getCourseSearchHistory(Context context) {
        if (null == context) {
            return "";
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        return pref.getString(COURSE_SEARCH_HISTORY, "");
    }

    public static void setCoursePickSearchStr(Context context, String searchStr) {
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        Editor editor = pref.edit();
        editor.putString(COURSE_PICK_SEARCH_STR, searchStr);
        editor.apply();
    }
    public static String getCCoursePickSearchStr(Context context) {
        if (null == context) {
            return "";
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        return pref.getString(COURSE_PICK_SEARCH_STR, "");
    }

}
